package demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.androideasy.permissions.EasyPermissions
import com.androideasy.permissions.Permission
import com.androideasy.permissions.PermissionCallback
import com.androideasy.permissions.demo.R

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<View>(R.id.btn_main_request_single).setOnClickListener(this)
        findViewById<View>(R.id.btn_main_request_group).setOnClickListener(this)

    }

    override fun onClick(view: View) {
        val viewId = view.id
        if (viewId == R.id.btn_main_request_single) {
            EasyPermissions.with(this)
                .permission(Permission.CAMERA)
                .request(object : PermissionCallback  {
                    override fun onGranted(permissions: List<String>, all: Boolean) {
                        if (all) {
                            toast("获取拍照权限成功")
                        }
                    }
                })
        } else if (viewId == R.id.btn_main_request_group) {
            EasyPermissions.with(this)
                .permission(Permission.RECORD_AUDIO)
                .permission(Permission.Group.CALENDAR)
                .request(object : PermissionCallback {
                    override fun onGranted(permissions: List<String>, all: Boolean) {
                        if (all) {
                            toast("获取录音和日历权限成功")
                        }
                    }
                })
        }
    }
    fun toast(text: CharSequence?) {
        Toast.makeText(this,text,Toast.LENGTH_SHORT).show()
    }
}